Editorial: Introduce an operation for backing To{Int,Uint}{8,16,32} and To{BigInt,BigUint}64#3832
Editorial: Introduce an operation for backing To{Int,Uint}{8,16,32} and To{BigInt,BigUint}64#3832
Conversation
…,32} and To{BigInt,BigUint}64
Ref tc39#3792
| _signed_: ~unsigned~ or ~signed~, | ||
| _bitWidth_: a positive integer, | ||
| _type_: ~number~ or ~bigint~, |
There was a problem hiding this comment.
These three arguments are partially redundant with what TypedArray element type describes in a single enumeration, but the latter also includes FLOAT{16,32,64} and I couldn't find a clean way to layer separation of just the INT/BIGINT values so I've left that as a potential exercise for the future (but ideas for the present are nonetheless welcome).
I suspect that going that route would involve something like introducing ToFloat{16,32,64} operations, migrating the last four columns of that table into Abstract Operations along with Is{Unsigned,UnclampedInteger,BigInt}ElementType for use by ToFixedSizeInteger, and simplifying e.g. NumericToRawBytes/NumericToRawBytes/etc.
There was a problem hiding this comment.
Three separate abstract operations seem a bit nicer, because it more clearly separates conversion from and to Number resp. BigInt.
ToFixedSizeInteger (
_argument_: an integer,
_signed_: ~unsigned~ or ~signed~,
_bitWidth_: a positive integer,
): an integer
1. Let _fixedInt_ be _int_ modulo 2<sup>_bitWidth_</sup>.
1. If _signed_ is ~signed~ and _fixedInt_ ≥ 2<sup>_bitWidth_ - 1</sup>, return _fixedInt_ - 2<sup>_bitWidth_</sup>.
1. Return _fixedInt_.
ToFixedSizeNumber(
_argument_: an ECMAScript language value,
_signed_: ~unsigned~ or ~signed~,
_bitWidth_: a positive integer,
): either a normal completion containing an integral Number or a throw completion
1. Let _int_ be ? ToIntegerOrInfinity(_argument_).
1. If _int_ is not finite, return 0.
1. Return 𝔽(ToFixedSizeInteger(_int_, _signed_, _bitWidth_)).
ToFixedSizeBigInt(
_argument_: an ECMAScript language value,
_signed_: ~unsigned~ or ~signed~,
_bitWidth_: a positive integer,
): either a normal completion containing a BigInt or a throw completion
1. Let _int_ be ℝ(? ToBigInt(_argument_)).
1. Return ℤ(ToFixedSizeInteger(_int_, _signed_, _bitWidth_)).
There was a problem hiding this comment.
Thanks for the suggestion. I took a half step in this direction, keeping the single new operation but making it infallibly map extended mathematical values and moving initial conversion to call sites.
|
The rendered spec preview for this PR is available as a single page at https://tc39.es/ecma262/pr/3832 and as multiple pages at https://tc39.es/ecma262/pr/3832/multipage . |
| ToInt32 ( | ||
| _argument_: an ECMAScript language value, | ||
| ): either a normal completion containing an integral Number or a throw completion | ||
| ToFixedSizeInteger ( |
There was a problem hiding this comment.
Since this now performs no type conversion, "IntegerToFixedSize" might be a better name. Opinions are welcome.
michaelficarra
left a comment
There was a problem hiding this comment.
LGTM otherwise. Great readability improvement.
There was a problem hiding this comment.
I'd prefer us to retain something like the example from the old note. Something like ToFixedSizeInteger(ToFixedSizeInteger(_x_, ~signed~, 32), ~unsigned~, 32) is the same as ToFixedSizeInteger(_x_, ~unsigned~, 32).
There was a problem hiding this comment.
| 1. If _int_ is either +∞ or -∞, return 0. |
(editorial convention)
There was a problem hiding this comment.
To be clear (despite GitHub bug), my suggestion is to insert "either".
There was a problem hiding this comment.
I find the comparison conventions terribly confusing/incomplete and possibly contradictory, but since int is a mathematical value, shouldn't this actually be If _int_ = +∞ or _int_ = -∞, return 0. as seen at BinaryOr, BigIntBitwiseOp, and IsWordChar? I almost always see the "is either $A or $B" form with $A and $B being language values and spec enums.
There was a problem hiding this comment.
Technically, it's an 'extended mathematical value', which the comparison conventions don't mention, but lumping it in with mathematical values would be reasonable. So yeah, using = seems more consistent.
Ref #3792
As suggested in #3792, ToFixedSizeInteger provides a single place to comment on the invariants of fixed-size integer conversion. However, in this PR it joins the supported operations in the existing Type Conversion section rather than splitting them out into a new subsection.